package com.woniuxy.jiapei.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.jiapei.pojo.*;
import com.woniuxy.jiapei.service.*;
import com.woniuxy.jiapei.util.R;
import com.woniuxy.jiapei.vo.BedQuery;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.jiapei.pojo.Bed;
import com.woniuxy.jiapei.service.BedService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lr
 * @since 2020-10-21
 */
@RestController
@RequestMapping("/jiapei/bed")
public class BedController {
    @Autowired
    private BedService bedService;

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalDeptService deptService;
    @Autowired
    private UserService userService;
    @Autowired
    private HosDeptService hosDeptService;
    @GetMapping("bed")
    @ApiOperation("查询所有的床")
    @CrossOrigin
    public R getAllBeds(@ApiParam("当前页")@RequestParam(value = "page",required = true,defaultValue = "1") Integer page,
                        @ApiParam("每页数据")@RequestParam(value = "limit",required = true,defaultValue = "5") Integer limit,
                        @ApiParam("开始时间")String value3, @ApiParam("结束时间") String value4, BedQuery bedQuery
                        ){
        String start ="00:00:00";
        String end ="23:59:59";
        //System.out.println(value3+value4);
        //System.out.println(value3.substring(11));
        Page waperPage =new Page(page,limit);
        QueryWrapper queryWrapper1 =new QueryWrapper();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        if(!StringUtils.isEmpty(value3)){
            value3 =value3.substring(0,11)+start;
            LocalDateTime localDate1 = LocalDateTime.parse(value3,formatter);
            Date date1 = Date.from( localDate1.atZone( ZoneId.systemDefault()).toInstant());
            queryWrapper1.ge("add_time",date1);
        }
        if(!StringUtils.isEmpty(value4)) {
            value4 =value4.substring(0,11)+end;
            LocalDateTime localDate2 = LocalDateTime.parse(value4,formatter);
            Date date2 = Date.from( localDate2.atZone( ZoneId.systemDefault()).toInstant());
            queryWrapper1.le("add_time", date2);
        }
        if(bedQuery.getId()!=null){
            queryWrapper1.eq("id",bedQuery.getId());
        }
        if(bedQuery.getStatus()!=null){
            queryWrapper1.eq("status",bedQuery.getStatus());
        }
        if(!StringUtils.isEmpty(bedQuery.getHosName())){
            queryWrapper1.eq("hos_name",bedQuery.getHosName());
        }
        if(!StringUtils.isEmpty(bedQuery.getHosTypeName())){
            queryWrapper1.eq("hos_type_name",bedQuery.getHosTypeName());
        }
       Page rPage=bedService.page(waperPage,queryWrapper1);
        List<Bed> list = rPage.getRecords();
        List<Hospital> hospitals=hospitalService.list();
        List<HospitalDept> deptList= deptService.list();
        long total =rPage.getTotal();
        if(list.size()>0&&list!=null){
            return R.ok().data("list",list).data("total",total).data("hospitals",hospitals).data("deptList",deptList);
        }else {
            return R.error();
        }
    }

    @ApiOperation("删除一张床")
    @CrossOrigin
    @DeleteMapping("bed/{id}")
    public R deleteOneBed(@PathVariable("id") Integer id){
        if(id!=null){
            bedService.removeById(id);
            return R.ok().code(200);
        }else {
            return R.error();
        }
    }

    @ApiOperation("查一张床")
    @CrossOrigin
    @GetMapping("bed/{id}")
    public R getOneBed(@PathVariable("id") Integer id){
        BedQuery bedQuery = new BedQuery();
        if(id!=null){
            Bed b =bedService.getById(id);
            if (b!=null) {
                User user = userService.getById(b.getUserId());
                bedQuery.setBed(b);
                bedQuery.setName(user.getName());
                bedQuery.setPhone(user.getPhone());
                return R.ok().data("bed", bedQuery);
            }else {
                return R.error();
            }
        }else {
            return R.error();
        }
    }

    @ApiOperation("显示对应医院的科室")
    @CrossOrigin
    @GetMapping("hosdept")
    public  R showHosAndType(Integer hosId){
        System.out.println(hosId);
        List<Hospital> hospitals=hospitalService.list();
        QueryWrapper wrapper =new QueryWrapper();
        List<HospitalDept> hosDepts=null;
        if(hosId!=null){
            wrapper.eq("hos_id",hosId);
            List<HosDept> htlist =hosDeptService.list(wrapper);
            if(htlist!=null&&htlist.size()>0){
                System.out.println("HOSDEPT的实体集合：--"+htlist);
                List<Integer> deptid =new ArrayList<>();
                for (HosDept o:htlist
                ) {
                    deptid.add(o.getDeptId());
                }
                System.out.println("科室ID集合：--"+deptid);
                hosDepts= deptService.listByIds(deptid);
            }
        }
        return R.ok().data("hospitals",hospitals).data("hosDepts",hosDepts);
    }

    @ApiOperation("添加一张床")
    @CrossOrigin
    @PostMapping("bed")
    public  R addBed(@RequestBody Bed bed) {
        if (bed != null) {
            bedService.save(bed);
        }
        return R.ok().code(200);
    }
}

